package com.base.baseweb;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class FetchPostProcessorLoader {

    private final Map<String, FetchPostProcessorFactory> factories = new HashMap<>();

    public FetchPostProcessorLoader(List<FetchPostProcessorFactory> factories) {
        factories.forEach(f -> this.factories.put(f.name(), f));
    }


    public FetchPostProcessor load(String name, Map<String, Object> config) {
        FetchPostProcessorFactory factory = factories.get(name);
        if (factory == null) {
            throw new IllegalArgumentException("Unknown post processor: " + name);
        }
        return factory.apply(factory.resolve(config));
    }
}
